import { Component, OnInit } from '@angular/core';
import { Title } from '@angular/platform-browser';
import { ActivatedRoute } from '@angular/router';
import { Location } from '@angular/common';
import { EntityPageResult } from 'src/app/model/entity-page-result';
import { MemberVo } from 'src/app/model/member-vo';
import { ThreadsCollectStatsVo } from 'src/app/model/threads-collect-stats-vo';
import { BtnNaviSharedService } from 'src/app/services/btn-navi-shared.service';
import { MemberStorageService } from 'src/app/services/member-storage.service';
import { ThreadsService } from 'src/app/services/threads.service';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { environment } from 'src/environments/environment';
@Component({
  selector: 'app-personal-reply-topic-result',
  templateUrl: './personal-reply-topic-result.component.html',
  styles: [``]
})
export class PersonalReplyTopicResultComponent implements OnInit {
  public curMember: MemberVo | null;
  //当前页码值
  public page: number = 1;
  //每页显示的数量
  public pageSize: number = 20;
  public pageResult$!: Observable<EntityPageResult<ThreadsCollectStatsVo>>;
  //当前的URL,用以浏览历史:window.history.pushState
  private url: string = '#';

  constructor(
    private title: Title,
    private location: Location,
    private activatedRoute: ActivatedRoute,
    private threadsService: ThreadsService,
    private memberStorage: MemberStorageService,
    private btnNaviSharedService: BtnNaviSharedService) {
    this.curMember = this.memberStorage.get();
    this.btnNaviSharedService.active(false);//不需要全局的导航
    this.activatedRoute.url.pipe(map(s => s.join('/'))).subscribe(res => {
      this.url = '/' + res;
    });
  }

  ngOnInit(): void {
    this.title.setTitle('个人中心:回复列表');
    this.page = +(this.activatedRoute.snapshot.queryParamMap.get('p') ?? 1);
    this.pageSize = +(this.activatedRoute.snapshot.queryParamMap.get('s') ?? environment.pageSize);
    this.getDate();
  }

  //加载指定页码的数据
  private getDate(): void {
    this.pageResult$ = this.threadsService.getAllForMemberReply(this.curMember?.id ?? 0, this.page, this.pageSize);
  }

  //底部的点击加载更多,后期会替换为:自动加载
  public anymore(event: Event): void {
    this.page += 1;
    this.getDate();
    this.location.replaceState(this.url, 'p=' + this.page + '&s=' + this.pageSize);
  }
}
